/**
 * Time：2024.04.28
 * Content：call_once使用场景
 * Author：Qian You
 */
#include <iostream>
#include <bits/stdc++.h>
// 头文件
#include <thread>
using namespace std;

static once_flag once;

class Log
{
public:
    Log(){};
    Log(const Log &log) = delete;
    Log &operator=(const Log &log) = delete;

    static Log &GetInstance()
    {
        // 饿汉模式
        // static Log *logs = nullptr;
        // if (!logs)
        //     logs = new Log;

        // 懒汉模式
        // static Log *logs = new Log;
        
        call_once(once, init);
        return *logs;
    }

    static void init()
    {
        if (!logs)
            logs = new Log;
    }
    void Printlog(string msg)
    {
        cout << __TIME__ << " " << msg << endl;
    }

private:
static Log* logs;
};

Log* Log::logs = nullptr;

void print_error()
{
    Log::GetInstance().Printlog("error");
}

int main()
{
    thread t1(print_error);
    thread t2(print_error);
    t1.join();
    t2.join();
    return 0;
}